/**
 * 
 */
package japavo.gui;

import japavo.gui.components.file_chooser.SaveFileChooser;
import japavo.gui.components.file_chooser.AbstractFileChooser.FileType;
import japavo.gui.general_ui.EditVocabularyBookPanel;
import japavo.gui.general_ui.MenuBar;
import japavo.gui.general_ui.StartPanel;
import japavo.gui.general_ui.TrainingPanel;
import japavo.tools.Logger;
import japavo.trainer.VocabularyTrainer;

import java.awt.Dimension;
import java.awt.Toolkit;

import javax.swing.BoxLayout;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;


/**
 *
 */
@SuppressWarnings("serial")
public class MainFrame extends JFrame {

	/**********************************************************************************************
	 ************************************ DECLARATIONS ******************************************** 
	 *********************************************************************************************/
	private VocabularyTrainer trainer;
	private JPanel visiblePanel;
	
	/**********************************************************************************************
	 ************************************ CONSTRUCTORS ******************************************** 
	 *********************************************************************************************/
	/**
	 * 
	 */
	public MainFrame(VocabularyTrainer trainer) {
		this.trainer = trainer;
		setTitle(System.getProperty("PROGRAMM_NAME") + " (v" + System.getProperty("PROGRAMM_VERSION") + ")");
		
		// Init logger
		Logger.init(this);
		
		// Calculate size and position
		Dimension frameSize = new Dimension(500, 350);
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		int left = (screenSize.width - frameSize.width) / 2;
		int top = (screenSize.height - frameSize.height) / 2;
		
		// Set size and position
		setPreferredSize(frameSize);
		setMinimumSize(frameSize);
		setLocation(left, top);
		
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		
		// set layout
		BoxLayout layout = new BoxLayout(getContentPane(), BoxLayout.PAGE_AXIS);
		getContentPane().setLayout(layout);
		
		// set default panel
		visiblePanel = new JPanel();
		
		setJMenuBar(new MenuBar(this, trainer));
		getContentPane().add(visiblePanel);
		
		// show the frame
		pack();
		validate();
		setVisible(true);
	}
	
	/**********************************************************************************************
	 ************************************** FUNCTIONS ********************************************* 
	 *********************************************************************************************/
	public void newVocabularyBook() {
		trainer.createNewVocabularyBook();
		setVisiblePanel(new EditVocabularyBookPanel(this, trainer));
	}
	
	/**
	 * Open a vocabulary book
	 */
	public void openVocabularyBook() {
		// Show file chooser
		JFileChooser chooser = new JFileChooser();
		int result = chooser.showOpenDialog(this);
		if (result != JFileChooser.APPROVE_OPTION) 
			return;
		
		// Open book and set visible panel
		trainer.openVocabularyBook(chooser.getSelectedFile());
		setVisiblePanel(new StartPanel(this, trainer));
	}
	
	/**
	 * Close the vocabulary book
	 */
	public void closeVocabularyBook() {
		trainer.closeVocabularyBook();
		setVisiblePanel(new JPanel());
	}
	
	/**
	 * Edits an active vocabulary book
	 */
	public void editVocabularyBook() {
		setVisiblePanel(new EditVocabularyBookPanel(this, trainer));
	}
	
	/**
	 * Save the vocabulary book
	 */
	public void saveVocabularyBook() {
		// Not saved yet => Dialog
		if (trainer.getActiveFile() == null) {
			SaveFileChooser chooser = new SaveFileChooser(FileType.VOC, System.getProperty("DIALOG_SAVE_HEADER"));
			chooser.showSaveDialog(this);
			if (chooser.getSelectedFile() == null)
				return;
			trainer.setActiveFile(chooser.getSelectedFile());
		}
		trainer.saveVocabularyBook();
	}
	
	public void startTraining() {
		TrainingPanel training = new TrainingPanel(this, trainer); 
		setVisiblePanel(training);
		trainer.startTraining(training);
	}
	
	public void getTrainingStats() {
		
	}
	
	private void setVisiblePanel(JPanel panel) {
		if (visiblePanel == null)
			return;
		visiblePanel.removeAll();
		visiblePanel.add(panel);
		visiblePanel.setLayout(new BoxLayout(visiblePanel, BoxLayout.PAGE_AXIS));
		validate();
		visiblePanel.setVisible(true);
	}
	
	
}